package cn.com.callback

import cn.com.elite.Util;
import cn.com.kh.KHDBConfig
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.elite.EliteBean;
import cn.com.kh.KHUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import java.text.SimpleDateFormat;

/**
 * 常规Groovy自动任务模板TODO部分填充业务规则两者配置定时的时候有区别
 */

public class auto7MoorAddCall implements CallBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(auto7MoorAddCall.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String paramString) {

        String AccountID = KHDBConfig.getParam(eliteBean, "7M-ACC");
        String APISecret = KHDBConfig.getParam(eliteBean, "7M-KEY");
        String APIURL = KHDBConfig.getAPIURL(eliteBean, "7Moor-addCallEvent");

        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        // 处理主数据
        String selectSQL = "Select Rec_Guid, Eventtype, Concretetype, Url, Name, Pbx, Eventtoken, Eventparam,Status From Kh_Addcallevent_Config Where Status = 1 And (Lastaddstatus = 0 Or Timestampdiff(Second, Lastaddtime, Now()) > 86400)";
        List<Map<String, Object>> selectCus = jt.queryForList(selectSQL);

        for (int i = 0; i < selectCus.size(); i++) {
            int LastAddStatus = 0;
            String Rec_Guid = selectCus.get(i).get("Rec_Guid").toString();
            try {
                String Eventtype = selectCus.get(i).get("Eventtype").toString();
                String Concretetype = selectCus.get(i).get("Concretetype").toString();
                String Url = selectCus.get(i).get("Url").toString();
                String Name = selectCus.get(i).get("Name").toString();
                String Pbx = selectCus.get(i).get("Pbx").toString();
                String Eventtoken = selectCus.get(i).get("Eventtoken").toString();
                String Eventparam = selectCus.get(i).get("Eventparam").toString();

                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String date = sdf.format(new Date());

                String bBase64 = AccountID + ":" + date;
                String Authorization = KHUtil.getbase64(bBase64);

                // 处理头文件
                Map<String, String> headerMap = new HashMap<String, String>();
                headerMap.put("Accept", "application/json");
                headerMap.put("Content-Type", "application/json");
                headerMap.put("Authorization", Authorization);

                // sig的值为 32位大写MD5加密 （帐号Id + 帐号APISecret +时间戳）
                String bSign = AccountID + APISecret + date;
                String sign = KHUtil.getMD5(bSign).toUpperCase();
                log.debug("sig的值:" + sign);

                String postURL = APIURL + "/" + AccountID + "?sig=" + sign;
                JSONObject postBody = new JSONObject();
                postBody.put("eventType", Eventtype);
                postBody.put("concreteType", Concretetype);
                postBody.put("url", Url);
                postBody.put("name", Name);
                postBody.put("pbx", Pbx);
                postBody.put("eventToken", Eventtoken);
                postBody.put("eventParam", Eventparam);
                log.debug("发送请求体：" + postBody.toString());

                // 调用API
                String postResult = Util.doHttpPostUseHead(postURL, postBody.toString(), "UTF-8", 120000, 0, headerMap);
                log.debug("返回请求体：" + postResult);

                JSONObject resultJson = JSONObject.fromObject(postResult);

                boolean Succeed = resultJson.getBoolean("Succeed");
                LastAddStatus = Succeed ? 1 : 0;

            } catch (Exception e) {
                log.error("当前Kh_Addcallevent_Config异常-:" + e.getMessage());
            }finally{

                String updateSQL = "Update Kh_Addcallevent_Config Set Lastaddtime = Now(), Lastaddstatus = ? Where Rec_Guid = ?";
                jt.update(updateSQL, LastAddStatus, Rec_Guid);
            }
        }

        log.debug("###map:" + map.toString());

    }

}
